// display the S-matrices
// the data format is in 'b Re(s) Im(s) |s|^2'
// by Asia Sun, Jul. 8, 2022

static int co = 0; // graph color
void primp(TGraph *g); // primp the graph
// get the ymin and ymax of a file with two columns (x, y)
void mm(const string &file, double &mi, double &ma);

void sm(){
  TLegend *lg = new TLegend(0.5, 0.15, 0.89, 0.45); lg->SetBorderSize(0);
  // for Sv(b) //
  TGraph *gvr = new TGraph("sv.txt", "%lg %lg %l*g %*lg");
  primp(gvr); lg->AddEntry(gvr, "Re(S_{v})");
  TGraph *gvi = new TGraph("sv.txt", "%lg %*lg %lg %*lg");
  primp(gvi); lg->AddEntry(gvi, "Im(S_{v})");
  TGraph *gv2 = new TGraph("sv.txt", "%lg %*lg %*lg %lg");
  primp(gv2); lg->AddEntry(gv2, "|S_{v}|^{2}");
  // for Sc(b) //
  TGraph *gcr = new TGraph("sc.txt", "%lg %lg %l*g %*lg");
  primp(gcr); lg->AddEntry(gcr, "Re(S_{c})");
  TGraph *gci = new TGraph("sc.txt", "%lg %*lg %lg %*lg");
  primp(gci); lg->AddEntry(gci, "Im(S_{c})");
  TGraph *gc2 = new TGraph("sc.txt", "%lg %*lg %*lg %lg");
  primp(gc2); lg->AddEntry(gc2, "|S_{c}|^{2}");

  const int n = gv2->GetN();
  double x, y1, y2, y;
  TGraph *gp = new TGraph(); // (1-|Sv|^2)*|Sc|^2
  primp(gp); lg->AddEntry(gp, "(1-|S_{v}|^{2})*|S_{c}|^{2}*200");
  for(int i = 0; i < n; i++){
    gv2->GetPoint(i, x, y1); gc2->GetPoint(i, x, y2);
    y = (1.-y1)*y2*200.; gp->SetPoint(i, x, y);
  } // end for over i

  TCanvas *c = new TCanvas("c", "c", 800, 600);
  gcr->SetMinimum(-1.1); gcr->SetMaximum(1.1);
  gcr->Draw(); gci->Draw("same"); gc2->Draw("same");
  gvr->Draw("same"); gvi->Draw("same"); gv2->Draw("same");
  gp->Draw("same");
  lg->Draw();

  c->Print("sm.pdf");
} // end main function mom

// primp the graph
void primp(TGraph *g){
  TAxis *ax = g->GetXaxis(); ax->SetTitle("b [fm]"); ax->CenterTitle();
  TAxis *ay = g->GetYaxis(); ay->SetTitle("S(b)"); ay->CenterTitle();
  co++;
  if(5 == co || 10 == co) co++;
  g->SetLineColor(co); g->SetMarkerColor(co); g->SetLineWidth(2);
} // end function primp

// get the ymin and ymax of a file with two columns (x, y)
void mm(const string &file, double &mi, double &ma){
  ifstream f(file); double x, y;
  while(f >> x >> y){ mi = min(mi, y); ma = max(ma, y); }
} // end main function
